<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/13
 * author : weblinuxgame
 * filename: ResponseLoginSuccess.php
 */

namespace App\Api\Responses\Login;

use App\Core\Enums\ParamsEnum;
use App\Models\User;
use Illuminate\Support\Arr;
use OpenApi\Annotations as OA;
use App\Api\Responses\ApiResponse;

/**
 * @OA\Schema(
 *     schema="LoginSuccessResponse",
 *     description="登陆成功响应",
 *     title="LoginSuccessResponse",
 *     @OA\Property(property="code",type="string",description="响应代码,异常码"),
 *     @OA\Property(property="message", type="string", description="响应结果提示"),
 * )
 * Class ResponseLoginSuccess
 * @package App\Api\Responses\Login
 */
class Success extends ApiResponse
{

    /**
     * @OA\Property(
     *     format="object",
     *     title="data",
     *     description="用户登陆成功数据",
     *     ref="#/components/schemas/LoginSuccessData"
     * )
     * @var object
     */
    public $data;

    /**
     * 成功登陆
     * Success constructor.
     * @param $user
     * @param $token
     */
    public function __construct($user, $token)
    {
        if (!empty($token)) {
            $token = (string)$token;
        }
        if (!is_array($user) && !empty($user)) {
            if ($user instanceof User) {
                $user = $user->toArray();
                Arr::forget($user, ['password', 'deleted_at']);
            }
        }
        parent::__construct(['userInfo' => $user, ParamsEnum::Token => $token], 'ok', 200);
    }

}
